# 获取行情规则
from bct.tools.client.bct4_client import BCT4Client

quote_rules = [{
    'instrumentType': 'FUTURES',
    'quoteInstance': 'CLOSE',
    'quoteField': 'SETTLE'
}, {
    'instrumentType': 'ANY',
    'quoteInstance': 'CLOSE',
    'quoteField': 'CLOSE'}]

# 获取标的定价参数规则
instrument_param_rules = [{
    'instrumentId': 'ANY',
    'parameterGroupId': None
}]

# 模型规则：日历，一年天数，MC相关，希腊字母相关
_default_model_config = {
    'calendar': 'CNY',
    'daysInYear': 250,
    'mcNumPaths': 10000,
    'mcSeed': 1234,
    'greeksSpotPercentBump': 0.01,
    'greeksVolBump': 0.01,
    'greeksRBump': 0.0001,
    'greeksQBump': 0.0001,
    'greeksNextDay': '1B'
}

# 模型规则： 定价方法相关
model_rules = [
    {
        'productType': 'AUTOCALL',
        'productSubType': 'FUTURES_OPTION',
        'pricingModel': 'BLACK76',
        'pricingMethod': 'MC',  # 'CPPCORE' OR 'MC'
        **_default_model_config
    },
    {
        'productType': 'AUTOCALL',
        'productSubType': 'ANY',
        'pricingModel': 'BLACKSCHOLES',
        'pricingMethod': 'MC',
        **_default_model_config
    },
    {
        'productType': 'ANY',
        'productSubType': 'FUTURES_OPTION',
        'pricingModel': 'BLACK76',
        'pricingMethod': 'ANALYTIC',
        **_default_model_config
    },
    {
        'productType': 'ANY',
        'productSubType': 'ANY',
        'pricingModel': 'BLACKSCHOLES',
        'pricingMethod': 'ANALYTIC',
        **_default_model_config
    }]

if __name__ == '__main__':
    # uncomment the line below if you have any specific pe convertor delete
    # delete_pricing_environment_by_name('RISK')

    # instrument param group
    # all use the same
    client = BCT4Client('http://10.1.6.69', 'script', '123456a.')

    # 获取标的物参数时使用的定价分组

    param_group = 'RISK'  # 定价分组
    # param_group_id = get_param_group_ids()[param_group]
    param_group_id = client.get_param_group_ids()[param_group]
    instrument_param_rules[0]['parameterGroupId'] = param_group_id

    # trade params not really used
    # 获取按交易获取定价参数时的定价分组
    trade_param_rules = [{
        'tradeId': 'NEVER_USED',
        'parameterGroupId': param_group_id
    }]

    # 创建一个名为 MC_INIT 的定价环境
    pricing_environment_name = 'MC_INIT'
    client.create_pe(
        pricing_environment_name,
        [{
            'instrumentType': 'ANY',
            'quoteInstance': 'CLOSE',
            'quoteField': 'CLOSE'
        }],
        [{
            'instrumentId': 'ANY',
            'parameterGroupId': param_group_id
        }],
        trade_param_rules,
        model_rules)

    #
    # # 创建一个名为 CPPCORE_INIT 的定价环境
    pricing_environment_name = 'CPPCORE_INIT'  # autocall -> quad
    model_rules[0]['pricingMethod'] = 'CPPCORE'
    model_rules[1]['pricingMethod'] = 'CPPCORE'
    # create_pe(pricing_environment_name, model_rules)
    client.create_pe(
        pricing_environment_name,
        [{
            'instrumentType': 'ANY',
            'quoteInstance': 'CLOSE',
            'quoteField': 'CLOSE'
        }],
        [{
            'instrumentId': 'ANY',
            'parameterGroupId': param_group_id
        }],
        trade_param_rules,
        model_rules)
